<!--{template lyear:header_simple_start}-->
<link href="{MOD_PATH}/images/market.css?{VERHASH}" rel="stylesheet" media="all">
<style>
.app_upgradelist{
	margin: auto;
	position: relative;
}
.app_upgradelist .upgrade_progess{
	position: absolute;
	bottom:0;
	left: 0;
	width:0%;
	height: 10%;
}
.app_upgradelist .appicon {
    padding: 5px;
	float: left;
	padding-right: 5px;
}
.app_upgradelist .appicon img {
    max-width: 45px;
    max-height: 45px;
    margin: 0;
}
.app-info{
	display:inline-block;
}
.app-info .select-info{
	display:none;
}
</style>
<!--{template lyear:header_simple_end}-->
<main class="bs-main-container">
	<div class="container-fluid">
	  <div class="card" id="checkloading">
		<!--{if $list}-->
		<header class="card-header flex-container">
			<div class="float-start">
				<input class="form-check-input" type="checkbox" name="chkall" id="chkall">
				<a href="{MOD_URL}" class="dcolor">{lang total}<span class="num">$count</span>{lang ge}{lang app}</a>
				<div class="app-info">
					<div class="app-info select-info">
					{lang selected}<span class="num">0</span>{lang ge}{lang app}
					<a class="btn btn-round btn-primary " id="update_selected" title="{lang update_onekey}" href="javascript:;" onclick="upgrade_all();"><i class="mdi mdi-apple-keyboard-shift"></i> {lang update_onekey}</a>
					</div>
				</div>
      		</div>
			<div class="float-end">
				<button class="btn btn-round btn-primary" id="update_check" title="{lang app_upgrade_check_need_update}"  onclick="upgrade_check(this);" data-loading-text="{lang app_upgrade_check_need_update}">{lang app_upgrade_check_need_update2}</button>
			</div>
		</header>
		<div class="card-body">
			<div class="alert alert-warning alert-dismissible fade show" role="alert">
				<strong>{lang board_message}</strong>
				<ul class="mb-0">
					<li>升级应用时，请先备份好数据库和插件文件，以免升级过程中出现错误。</li>
					<li>升级后，需要前往系统工具中清理缓存。</li>
					<li>因升级会重置应用参数（非用户数据），所以升级后，需要重新配置应用。</li>
					<li>如不能在线升级，可以前往<a class="num" href="http://www.dzzoffice.com/index.php?mod=dzzmarket" target="_blank">官方应用市场</a>下载插件包进行手动升级。</li>
					<li>检测新版本应用时，只限检测已启用的应用，关闭的应用如需检测更新，需先将其切换为启用状态。</li>
				</ul>
				<button type="button" class="btn-close" data-dismiss="alert" aria-label="Close"></button>
			</div>
			<form id="appform" name="appform" class="form-horizontal" action="{MOD_URL}" method="post">
				<input type="hidden" name="appsubmit" value="true" />
				<input type="hidden" name="formhash" value="{FORMHASH}" />
				<div class="table-responsive">
					<table class="table table-hover align-middle">
						<thead>
						<tr>
							<th width="20px">#</th>
							<th width="50%">{lang application_name}</th>
							<th width="100px">{lang installed}</th>
							<th width="100px">{lang newest}</th>
							<th>{lang operation}</th>
						</tr>
						</thead>
						<tbody>
						<!--{loop $list $value}--> 
						<tr id="app_div_{$value[appid]}" class="app_upgradelist">
							<th scope="row">
								<input type="checkbox" name="del[]" class="form-check-input" value="$value[appid]" data-mid="$value[mid]">
								<label></label>
								<div id="progess_{$value[appid]}">
									<span id="upgrade_progess_{$value[appid]}" class="progress-bar progress-bar-striped progress-bar-animated upgrade_progess"></span> 
								</div>
							</th>
							<td>
								{eval $appadminurl=replace_canshu($value['appadminurl']?$value['appadminurl']:$value['appurl'])}
								<a href="$appadminurl" target="_blank" class="appicon"><img src="$value[appico]" style="margin:0" /></a><a class="dcolor" href="$appadminurl" target="_blank">$value[appname]</a>
								{eval $appdesc=$value['upgrade_version']['desc_short']?$value['upgrade_version']['desc_short']:lang('none');}
								<div class="appdesc form-text" title="$appdesc">
									$appdesc
								</div>
							</td>
							<td>$value[version]</td>
							<td>$value[upgrade_version][version]</td>
							<td>
								<a class="btn btn-round btn-primary btn-sm" id="upgrade_info_{$value[appid]}" href="javascript:;" onclick="start_check_upgrade('$value[appid]',1);" title="{lang update_onekey}">{lang update_onekey}</a>
							</td>
						</tr>
						<!--{/loop}-->
						</tbody>
					</table>
				</div>
				<!--{if $multi}-->
				$multi
				<!--{/if}-->
			</form>
		</div>
		<!--{else}-->
		<div class="card-body">
			<div class="alert alert-warning alert-dismissible fade show" role="alert">
				<strong>{lang board_message}</strong>
				<p class="mb-0">如不能检测新版本升级，可以前往<a class="num" href="http://www.dzzoffice.com/index.php?mod=dzzmarket" target="_blank">官方应用市场</a>下载插件包进行手动升级。</p>
				<button type="button" class="btn-close" data-dismiss="alert" aria-label="Close"></button>
			</div>
			<div class="text-center">
				<img src="static/image/common/no_list.png" /><br>
				<span class="lead">还没有可以升级的应用…</span><br>
				<button class="btn btn-primary btn-round" id="update_check" title="{lang app_upgrade_check_need_update}"  onclick="upgrade_check(this);" data-loading-text="{lang app_upgrade_check_need_update}">{lang app_upgrade_check_need_update2}</button>
			</div>
		</div>
		<!--{/if}--> 
	</div>
</div>
</main>
<script type="text/javascript">
	var upgrade=false;
	var appids=[];
	var mids=[];
	var nowupgradeappid=0;
	jQuery('input[name="del[]"]').on('change',function(){
		//console.log('change==='+this.value);
		checkSelected();
	});
	jQuery('#chkall').on('change',function(){
		if(jQuery(this).prop('checked')){
			jQuery('input[name="del[]"]').prop('checked',true);
		}else{
			jQuery('input[name="del[]"]').prop('checked',false);
		}
		checkSelected();
	});
	
	function checkSelected(){
		var i=0;
		appids=[];
		mids=[];
		jQuery('input[name="del[]"]').each(function(){
			//console.log(this);
			if(jQuery(this).prop('checked')){
				appids.push(this.value);
				mids.push(jQuery(this).data("mid"));
			}
			i++;
		});
		var num=appids.length;
		//console.log([i,num]);
		if(num>0){
			jQuery('.select-info').show().find('.num').text(num);
			if(i>0 && i==num){//全部选中时
				jQuery('#chkall').prop('checked',true);
			}else{
				jQuery('#chkall').prop('checked',false);
			}
		}else{
			jQuery('.select-info').hide().find('.num').text('0');
		}
	}
	function upgrade_check(obj){//强制检测更新，不受一天一次的限制，一般用于刚上传或修改了应用的配置信息，通过这个按钮可以强制刷新出需要更新的应用
		var loader = $('#checkloading').lyearloading({
					opacity: 0.2,
					spinnerSize: 'lg',
					spinnerText: '正在检测新版本中',
					textColorClass: 'text-info',
					spinnerColorClass: 'text-info',
					zindex: 9,
				});
		jQuery(obj).button('loading');
		jQuery.post('{MOD_URL}&op=check_upgrade',function(json){
			var oldsum=parseInt(jQuery('#update_app_num').text()); 
			if(json.sum!=oldsum){
				window.location.reload();
			}else{
				loader.destroy();
				showmessage('没有检测到新的更新','info',1000,1);
			}
			jQuery(obj).button('reset');
		},'json')
		.fail(function (jqXHR, textStatus, errorThrown) {
			loader.destroy();
			// 这里处理请求失败的情况
			showmessage('请求失败: ' + textStatus, 'error', 3000, 1);
		});
	}
	function upgrade_all(){ 
		var num=appids.length;
		if( upgrade ){
			alert("正在升级,请稍等...");
			return false;
		}
		if(num>0){
			start_check_upgrade( 0 ,2); 
		}
	}
	
	function start_check_upgrade(appid,s){
		var url="";
		var appid = parseInt(appid);
		var url_s='{MOD_URL}&op=upgrade_app_ajax&operation=check_upgrade&appid='; 
		if( upgrade ){
			//alert("正在升级,请稍等...");
			return false;
		}
		upgrade=true;
		if( s==2 ){
			if(nowupgradeappid==0 && appids.length>0 ){
				url=url_s+appids[0]; 
				nowupgradeappid=appids[0];
				appids.shift();
			}else{ 
				url=url_s+nowupgradeappid; 
			} 
		}else{
			if( appid==0 || isNaN(appid) ){
				appid=nowupgradeappid; 
			}
			url=url_s+appid;
			nowupgradeappid=appid;
		}
		
		jQuery.ajax({
			type:'GET',
			async: false, 
			url:url,
			data:{},
			success:function(json){
				if(json.status==0){
					jQuery('#upgrade_info_'+nowupgradeappid).html(json.msg);
					nowupgradeappid=0;
					upgrade=false; 
					if( appids.length>0 && s==2){ 
						nowupgradeappid=0;
						url=url_s+appids[0]; 
						start_check_upgrade(url,s);
					} 
				}else{
					jQuery('#upgrade_info_'+nowupgradeappid).html(json.msg);
					jQuery('#upgrade_progess_'+nowupgradeappid).animate({width:json.percent+"%"},json.second,function(){
						if(json.mid>0){
							startupgrade(json.url,s);
						}else{
							upgradeover(json.url,s);
						} 
					});
				}
			},
			error: function(xhr, status, error) {
				showmessage('Request failed:' + status +' ' +error, 'error', 3000, 1);
			}
		});
	}

	function startupgrade(url,s){
		jQuery.ajax({
			type:'GET',
			async: false, 
			url:url,
			data:{},
			success:function(json){ 
				if(json.status==0){
					jQuery('#upgrade_info_'+nowupgradeappid).html(json.msg);
					upgrade=false;
				}else{
					jQuery('#upgrade_info_'+nowupgradeappid).html(json.msg);
					jQuery('#upgrade_progess_'+nowupgradeappid).animate({width:json.percent+"%"},json.second,function(){
						startgetcrossorpatchfile(json.url,s);
					});
				}
			},
			error: function(xhr, status, error) {
				showmessage('Request failed:' + status +' ' +error, 'error', 3000, 1);
			}
		});
	}
	
	function startgetcrossorpatchfile(url,s){
		jQuery.ajax({
			type:'GET',
			async: false, 
			url:url,
			data:{},
			success:function(json){
				if(json.status==0){ 
					jQuery('#upgrade_info_'+nowupgradeappid).html(json.msg);
					upgrade=false; 
				}else{
					jQuery('#upgrade_info_'+nowupgradeappid).html(json.msg);
					jQuery('#upgrade_progess_'+nowupgradeappid).animate({width:json.percent+"%"},json.second,function(){
						if(json.step==2){
							startgetcrossorpatchfile(json.url,s);
						}else{
							startupgradefile(json.url,s);
						}
					}); 
				}
			},
			error: function(xhr, status, error) {
				showmessage('Request failed:' + status +' ' +error, 'error', 3000, 1);
			}
		});
	}
	
	function startupgradefile(url,s){
		jQuery.ajax({
			type:'GET',
			async: false, 
			url:url,
			data:{},
			success:function(json){
				if(json.status==0){ 
					upgrade=false; 
					jQuery('#upgrade_info_'+nowupgradeappid).html(json.msg);
				}else{
					jQuery('#upgrade_info_'+nowupgradeappid).html(json.msg);
					jQuery('#upgrade_progess_'+nowupgradeappid).animate({width:json.percent+"%"},json.second,function(){
						 if(json.step!=5){
							startupgradefile(json.url,s);
						}else{
							upgradeover(json.url,s);
						}
					}); 
				}
			},
			error: function(xhr, status, error) {
				showmessage('Request failed:' + status +' ' +error, 'error', 3000, 1);
			}
		});
	}
	
	function upgradeover(url,s){
		jQuery.ajax({
			type:'GET',
			async: false, 
			url:url,
			data:{},
			success:function(json){ 
				if(json.status==0){ 
					upgrade=false; 
					jQuery('#upgrade_info_'+nowupgradeappid).html(json.msg);
				}else{
					jQuery('#upgrade_info_'+nowupgradeappid).html(json.msg);
					jQuery('#upgrade_progess_'+nowupgradeappid).animate({width:"100%"},300,function(){
						jQuery('#upgrade_progess_'+nowupgradeappid).css("width","0%");
						upgrade=false; 
						start_check_upgrade(json.url,s);
					}); 
				}
			},
			error: function(xhr, status, error) {
				showmessage('Request failed:' + status +' ' +error, 'error', 3000, 1);
			}
		});
	}
</script>
<!--{template lyear:footer_simple}-->